﻿using A.M.Storage.Infrastruecure;
using L.M.System.Domain.Entity;
using Newtonsoft.Json;
using Public_Class_Library;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using W.M.Materiel.Domain.Entity;
using W.M.Materiel.Infrastruecure.DTO;
using W.M.Materiel.Infrastruecure.IRepository;

namespace W.M.Materiel.Infrastruecure.Repository
{
    public class Bill_of_materialsRepository : BaseRepository<bill_of_materials, string>, IBill_of_materialsRepository
    {
        private readonly ISqlSugarClient _client;
        private readonly Cross_service_Helper cross_Service_Helper;

        public Bill_of_materialsRepository(ISqlSugarClient client, Cross_service_Helper cross_Service_Helper) : base(client)
        {
            _client = client;
            this.cross_Service_Helper = cross_Service_Helper;
        }

        /// <summary>
        /// 物料信息+查询+分页
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="name">查询条件：物料名称</param>
        /// <param name="id">查询条件：单位id</param>
        /// <returns>物料信息</returns>
        public List<Bill_of_materialsDTO> GetBill_Of_Materials(int pageSize, int pageIndex, string? name, string? id, out int totalCount, out int pageCount)
        {


            var obj = cross_Service_Helper.Get("http://10.31.59.5:8001/api/Table_of_unitsRead/GetTableUrl").Result;
            var jsonObj = JsonConvert.DeserializeObject<List<table_of_units>>(obj);

            var plist = _client.Queryable<bill_of_materials>().ToList();

            var list = (from a in jsonObj
                        join b in plist
                        on a.unit_number equals b.unit_id
                        where (id == null || b.unit_id == id) &&
                        (string.IsNullOrEmpty(name) || b.material_name.Contains(name))
                        select new Bill_of_materialsDTO
                        {
                            material_name = b.material_name,
                            unit_id = a.unit_number,
                            material_attribute = b.material_attribute,
                            material_code = b.material_code,
                            material_life = b.material_life,
                            attachments = b.attachments,
                            auxiliary_unit = b.auxiliary_unit,
                            picture = b.picture,
                            status = b.status,
                            unit_name = a.unit_name,
                            alarm_days = b.alarm_days,
                            remark = b.remark,
                            serial_number = b.serial_number,
                            specification_model = b.specification_model,
                            purchase_price = b.purchase_price,
                            minimum_inventory = b.minimum_inventory,
                            maximum_inventory = b.maximum_inventory,
                            brand = b.brand,
                            number_parts = 1
                        }).ToList();

            totalCount = list.Count();
            pageCount = (int)Math.Ceiling(totalCount * 1.0 / pageSize); 
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            return list;
        }


    }
}
